function mapAvg = mapAverage(mapStack, NaNoption)
% syntax:       mapAvg = mapAverage(mapStack, NaNoption)
% takes a stack of maps and returns an average of all the planes
% call with NaNoption set to 1 to avg across NaNs
%
% Editing:
% gs may 2005 -- private version for mapAverager1p0
% -----------------------------------------------------------------
[rows, cols, planes] = size(mapStack);

if nargin <2
    NaNoption = 1;
end

if NaNoption
    for r = 1:rows
        for c = 1:cols
            planeVec = mapStack(r,c,:);
            if any(isfinite(planeVec))
                mapAvg(r,c) = mean(planeVec(~isnan(planeVec)));
            else
                mapAvg(r,c) = NaN;
            end
        end
    end
else
    for r = 1:rows
        for c = 1:cols
            mapAvg(r,c) = mean(mapStack(r,c,:));
        end
    end
end

if planes < 2
    beep; disp('Only 1 map array sent to mapAverage')
end
